﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDemo.Filter
{
    /// <summary>
    /// 自定义动作过滤器
    /// </summary>
    public class CustomActionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// Action执行前
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //1.获取获取请求的类名和方法名
            string strController = filterContext.RouteData.Values["controller"].ToString();
            string strAction = filterContext.RouteData.Values["action"].ToString();

            //2.另一种方式获取请求的类名和方法名
            string strController2 = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string strAction2 = filterContext.ActionDescriptor.ActionName;

            filterContext.HttpContext.Response.Write("Action执行前<br/>");

            filterContext.HttpContext.Response.Write("控制器:" + strController + "</br>");
            filterContext.HttpContext.Response.Write("控制器:" + strController2 + "</br>");
            filterContext.HttpContext.Response.Write("Action:" + strAction + "</br>");
            filterContext.HttpContext.Response.Write("Action:" + strAction2 + "</br>");

            base.OnActionExecuting(filterContext);

        }

        /// <summary>
        /// Action执行后
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("Action执行后<br/>");
            base.OnActionExecuted(filterContext);
        }
    }
}